package com.example.demo.utils;

import com.alibaba.fastjson.JSON;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author xumf
 * @date 2019/9/19 15:34
 */
public class GZipUtils {
    // 压缩
    public static byte[] compress(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(data);
        gzip.close();
        return  out.toByteArray();//out.toString("ISO-8859-1");
    }

    //解压
    public static String unCompress(byte[] b) {
        try {
            if (null == b || b.length <= 0) {
                return null;
            }
            // 创建一个新的 byte 数组输出流
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            // 创建一个 ByteArrayInputStream，使用 buf 作为其缓冲区数组
            ByteArrayInputStream in;
            in = new ByteArrayInputStream(b);

            // 使用默认缓冲区大小创建新的输入流
            GZIPInputStream gzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n = 0;
            while ((n = gzip.read(buffer)) >= 0) {// 将未压缩数据读入字节数组
                // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此 byte数组输出流
                out.write(buffer, 0, n);
            }
            // 使用指定的 charsetName，通过解码字节将缓冲区内容转换为字符串
            return out.toString("UTF-8");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    // 压缩
    public static byte[] compresszip(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(out);
        zip.write(data);
        zip.close();
        return  out.toByteArray();//out.toString("ISO-8859-1");
    }


    public static void main(String[] args) throws IOException {
//        String fileName="G:/test.txt";
//        String str= ReadFileUitl.readFileIO(fileName);
        String str="{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"isShow\":false,\"lineColor\":\"red\",\"groupAll\":1,\"groupAllseries\":\"默认分组\",\"groupAllseriesItem\":\"点201910\"},\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[121.95648193359374,30.04532159026885],[122.28057861328124,30.047699084516957]]}},{\"type\":\"Feature\",\"properties\":{\"isShow\":true,\"fillColor\":\"red\",\"fillOutlineColor\":\"red\",\"groupAll\":1,\"groupAllseries\":\"默认分组\",\"groupAllseriesItem\":\"点201910\"},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[122.01690673828124,29.954934549656144],[121.95373535156249,29.871610558533725],[122.1954345703125,29.90494852052797],[122.01690673828124,29.954934549656144]]]}},{\"type\":\"Feature\",\"properties\":{\"isShow\":true,\"iconImage\":\"mr\",\"groupAll\":1,\"groupAllseries\":\"默认分组\",\"groupAllseriesItem\":\"点201910\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[122.2723388671875,29.943035391587742]}},{\"type\":\"Feature\",\"properties\":{\"isShow\":true,\"iconImage\":\"mr\",\"group\":2},\"geometry\":{\"type\":\"Point\",\"coordinates\":[122.29843139648436,30.016192655492063]}}]}";
        byte[] b= compress(str.getBytes("UTF-8"));
        System.out.println(JSON.toJSONString(b));
//        System.out.println(unCompress(b));


    }
}
